import React from 'react';
import { Row, Col, Form, InputNumber, Input, Button, Table, Select,Popover, Modal, message} from 'antd';
const R = require('ramda');
import BizParams from '../../../components/commons/BizParams'
import ManageSelect from '../../../components/commons/ManageSelect'
import RightBox from "../../commons/RightBox";
import RightConstants from "../../../utils/RightConstants";
import Constants from "../../../utils/Constants";
const FormItem = Form.Item;
const view= "view"

export default Form.create({wrappedComponentRef: true})(class PersonnelAllocation extends React.Component {
	constructor(props) {
		super(props);
        this.state = {

        }
	}

    handleResetFields = () => {
        this.props.form.resetFields();
    }

    handleUpdate = () => {
        this.props.form.validateFields((errors, values) => {
            let dataSource = R.isNil(this.props.dataSource) ? {} : this.props.dataSource
            if (errors) return;

            if(this.state.type == view){
                return;
            }

            this.props.dispatch({
                type: "customerManage/updateCustomerBase",
                payload: {
                    cstid : this.props.cstid,
                    id : dataSource.id,
                    manager : values.manager,
                    waiter : values.waiter,
                    tgmanager : values.tgmanager,
                    onSuccess:()=>{
                        this.props.dispatch({type: 'customerManage/getCustomerById', payload: {id: this.props.cstid, isApproval: false}});
                    }


                }
            })

            this.handleCancel();
        })
    }

	render() {
		const { getFieldDecorator } = this.props.form;
        const formItemLayout = {labelCol: { xs: {span: 8}, sm: {span: 8}}, wrapperCol: {xs: {span: 15}, sm: {span: 14}}};
        let dataSource = R.isNil(this.props.dataSource) ? {} : this.props.dataSource;

        let buttonDis = (!R.isNil(dataSource.waiter) && !R.isEmpty(dataSource.waiter)) && (!R.isNil(dataSource.tgmanager) && !R.isEmpty(dataSource.tgmanager))
            && (!R.isNil(dataSource.manager) && !R.isEmpty(dataSource.manager))//true
        let rights = R.isNil(sessionStorage.getItem(Constants.USERRIGHTS)) ? [] : sessionStorage.getItem(Constants.USERRIGHTS);
        buttonDis =false;
        buttonDis = buttonDis || !R.contains(RightConstants.CUSTOMER.CUSTOMER_DETAIL_CUSTOMER_ALLOCATE)(rights);

        // let gdmanager= R.isNil(this.props.manager) ? [] : this.props.manager.gdmanager;
        // let tgmanager= R.isNil(this.props.manager) ? [] : this.props.manager.tgmanager;
        // let ywmanager= R.isNil(this.props.manager) ? [] : this.props.manager.ywmanager;

        return (

                <Form>
                    <Row>
                        <Col span={9}>
                            <FormItem
                                {...formItemLayout}
                                label="跟单经理分配"
                                hasFeedback
                            >
                                {getFieldDecorator('waiter', {
                                    rules: [{
                                        required: true, message: '请选择跟单经理！',
                                    }],
                                    initialValue: dataSource.waiter
                                })(
                                    <ManageSelect sprCode={"gdmanager"} style={{width:'100%'}} blank={true} disabled={this.props.disabled|| buttonDis}/>

                                )}
                                {/*<Select style={{width: 150}} disabled={this.props.disabled|| buttonDis}>*/}
                                    {/*{*/}
                                        {/*R.isNil(gdmanager) || R.isEmpty(gdmanager)  ? <Option value=""> </Option>*/}
                                            {/*:*/}
                                            {/*(gdmanager).map(item => {*/}
                                                {/*return (<Option value={item.userId} key={item.userId}>{item.username}</Option>);*/}
                                            {/*})*/}
                                    {/*}*/}
                                {/*</Select>*/}
                            </FormItem>
                        </Col>
                        <Col span={9}>
                            <FormItem
                                {...formItemLayout}
                                label="业务经理分配"
                                hasFeedback
                            >
                                {getFieldDecorator('manager', {
                                    rules: [{
                                        required: true, message: '请选择业务经理！',
                                    }],
                                    initialValue: dataSource.manager
                                })(
                                    <ManageSelect sprCode={"ywmanager"} style={{width:'100%'}} blank={true} disabled={this.props.disabled|| buttonDis}/>

                                )}

                                {/*<Select style={{width: 150}} disabled={this.props.disabled|| buttonDis}>*/}
                                    {/*{*/}
                                        {/*R.isNil(ywmanager) || R.isEmpty(ywmanager)  ? <Option value=""> </Option>*/}
                                            {/*:*/}
                                            {/*(ywmanager).map(item => {*/}
                                                {/*return (<Option value={item.userId} key={item.userId}>{item.username}</Option>);*/}
                                            {/*})*/}
                                    {/*}*/}
                                {/*</Select>*/}
                            </FormItem>
                        </Col>
                    </Row>

                    <Row>

                        <Col span={9}>
                            <FormItem
                                {...formItemLayout}
                                label="通关经理分配"
                                hasFeedback
                            >
                                {getFieldDecorator('tgmanager', {
                                    rules: [{
                                        required: true, message: '请选择通关经理！',
                                    }],
                                    initialValue: dataSource.tgmanager
                                })(
                                    <ManageSelect sprCode={"tgmanager"} style={{width:'100%'}} blank={true} disabled={this.props.disabled|| buttonDis}/>

                                )}
                                {/*<Select style={{width: 150}} disabled={this.props.disabled|| buttonDis}>*/}
                                    {/*{*/}
                                        {/*R.isNil(tgmanager) || R.isEmpty(tgmanager)  ? <Option value=""> </Option>*/}
                                            {/*:*/}
                                            {/*(tgmanager).map(item => {*/}
                                                {/*return (<Option value={item.userId} key={item.userId}>{item.username}</Option>);*/}
                                            {/*})*/}
                                    {/*}*/}
                                {/*</Select>*/}
                            </FormItem>
                        </Col>
                        <Col span={9}>

                            {this.props.disabled|| buttonDis ? "" :
                            <Button type="primary" icon="className" size={'default'} disabled={this.props.disabled|| buttonDis}
                                    onClick={this.handleUpdate.bind(this)} style={{display: this.props.disabled ? 'none' : 'inline-block', marginLeft:10 }}>确认分配</Button>
                            
                            }

                        </Col>
                    </Row>
                </Form>
        );
	}
})


// WEBPACK FOOTER //
// ./public/src/components/order/components/PersonnelAllocation.js